package com.ocom.mqzhongyu.controller;


import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.MqZhongYuApiUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.IdentificationCodeUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mqzhongyu.business.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

@RestController
public class MqController {

	private Logger logger = LoggerFactory.getLogger(MqController.class);


	@Autowired
	private ProductService productService;

	@PostMapping(MqZhongYuApiUrl.URL_ZyApiMq)
	public Result Send(Long companyId, String identity, String name){
		identity=identity.toUpperCase(Locale.ROOT);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("companyId", companyId);
		jsonObject.put("identity", identity);
		jsonObject.put("name", name);

		logger.info("接收到mq請求 {}",jsonObject);

		if(!IdentificationCodeUtil.isIdentityCode(identity)){
			return ResultUtil.error(ResultEnum.Err_2021);
		}

		try {
			productService.makePayments(jsonObject.toJSONString());
		}catch (Exception e){
			logger.error("发送mq失败  错误【{}】",e);
			return ResultUtil.error(ResultEnum.Err_4006);
		}
//		Random random=new Random();
//		for (int i=1;i<10;i++){
//			Integer r=random.nextInt(9);
//			productService.makePayments("order "+r);
//		}
		return ResultUtil.success();
	}


}
